Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
driver_FXOS8700.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*! \file driver_FXOS8700.c
31  \brief Provides init() and read() functions for the FXOS8700 6-axis accel plus mag
32 */
33 
34 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
35 #include "sensor_fusion.h" // Sensor fusion structures and types
36 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
37 #include "fxos8700.h" // describes the FXOS8700 register definition and its bit mask
38 #include "fxos8700_drv.h" // Low level IS-SDK prototype driver
39 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
40 #include "status.h"
41 
42 #define FXOS8700_ACCEL_FIFO_SIZE 32 ///< FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO
43 #define FXOS8700_MAG_FIFO_SIZE 1 ///< FXOS8700 (mag), MAG3110 have no FIFO so equivalent to 1 element FIFO
44 
45 // Command definition to read the WHO_AM_I value.
46 const registerreadlist_t FXOS8700_WHO_AM_I_READ[] =
47 {
48  { .readFrom = FXOS8700_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
49 };
50 
51 // Command definition to read the number of entries in the accel FIFO.
52 const registerreadlist_t FXOS8700_F_STATUS_READ[] =
53 {
54  { .readFrom = FXOS8700_STATUS, .numBytes = 1 }, __END_READ_DATA__
55 };
56 
57 // Command definition to read the number of entries in the accel FIFO.
58 registerreadlist_t FXOS8700_DATA_READ[] =
59 {
60  { .readFrom = FXOS8700_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
61 };
62 
63 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
64 const registerwritelist_t FXOS8700_Initialization[] =
65 {
66  // Command definition to write 0000 0000 = 0x00 to CTRL_REG1 to place FXOS8700 into standby
67  // [7-1] = 0000 000
68  // [0]: active=0
69  { FXOS8700_CTRL_REG1, 0x00, 0x00 }, // write 0100 0000 = 0x40 to F_SETUP to enable FIFO in continuous (circular) mode
70 
71  // [7-6]: f_mode[1-0]=01 for FIFO continuous mode
72  // [5-0]: f_wmrk[5-0]=000000 for no FIFO watermark
73  { FXOS8700_F_SETUP, 0x40, 0x00 },
74 
75  // write 0001 1111 = 0x1F to M_CTRL_REG1
76  // [7]: m_acal=0: auto calibration disabled
77  // [6]: m_rst=0: one-shot magnetic reset disabled
78  // [5]: m_ost=0: one-shot magnetic measurement disabled
79  // [4-2]: m_os=111=7: maximum oversampling to reduce magnetometer noise
80  // [1-0]: m_hms=11=3: select hybrid mode with accel and magnetometer active
81  { FXOS8700_M_CTRL_REG1, 0x1F, 0x00 }, // write 0000 0000 = 0x00 to M_CTRL_REG2
82 
83  // [7]: reserved
84  // [6]: reserved
85  // [5]: hyb_autoinc_mode=0 to ensure address wraparound to 0x00 to clear accelerometer FIFO in one read
86  // [4]: m_maxmin_dis=0 to retain default min/max latching even though not used
87  // [3]: m_maxmin_dis_ths=0
88  // [2]: m_maxmin_rst=0
89  // [1-0]: m_rst_cnt=00 to enable magnetic reset each cycle
90  { FXOS8700_M_CTRL_REG2, 0x00, 0x00 },
91 
92  // write 0000 0001= 0x01 to XYZ_DATA_CFG register
93  // [7]: reserved
94  // [6]: reserved
95  // [5]: reserved
96  // [4]: hpf_out=0
97  // [3]: reserved
98  // [2]: reserved
99  // [1-0]: fs=01 for 4g mode: 2048 counts / g = 8192 counts / g after 2 bit left shift
100  { FXOS8700_XYZ_DATA_CFG, 0x01, 0x00 }, // write 0000 0010 = 0x02 to CTRL_REG2 to set MODS bits
101 
102  // [7]: st=0: self test disabled
103  // [6]: rst=0: reset disabled
104  // [5]: unused
105  // [4-3]: smods=00
106  // [2]: slpe=0: auto sleep disabled
107  // [1-0]: mods=10 for high resolution (maximum over sampling)
108  { FXOS8700_CTRL_REG2, 0x02, 0x00 },
109 
110  // write 00XX X101 = 0x0D to accelerometer control register 1
111  // since this is a hybrid sensor with identical accelerometer and magnetometer ODR the value for ACCEL_ODR_HZ is used
112  // [7-6]: aslp_rate=00
113  // [5-3]: dr=111 for 0.78Hz data rate giving 0x3D
114  // [5-3]: dr=110 for 3.125Hz data rate giving 0x35
115  // [5-3]: dr=101 for 6.25Hz data rate giving 0x2D
116  // [5-3]: dr=100 for 25Hz data rate giving 0x25
117  // [5-3]: dr=011 for 50Hz data rate giving 0x1D
118  // [5-3]: dr=010 for 100Hz data rate giving 0x15
119  // [5-3]: dr=001 for 200Hz data rate giving 0x0D
120  // [5-3]: dr=000 for 400Hz data rate giving 0x05
121  // [2]: lnoise=1 for low noise mode (since we're in 4g mode)
122  // [1]: f_read=0 for normal 16 bit reads
123  // [0]: active=1 to take the part out of standby and enable sampling
124 #if (ACCEL_ODR_HZ <= 1) // select 0.78Hz ODR
125  { FXOS8700_CTRL_REG1, 0x3D, 0x00 },
126 #elif (ACCEL_ODR_HZ <= 3) // select 3.125Hz ODR
127  { FXOS8700_CTRL_REG1, 0x35, 0x00 },
128 #elif (ACCEL_ODR_HZ <= 6) // select 6.25Hz ODR
129  { FXOS8700_CTRL_REG1, 0x2D, 0x00 },
130 #elif (ACCEL_ODR_HZ <= 30) // select 25Hz ODR
131  { FXOS8700_CTRL_REG1, 0x25, 0x00 },
132 #elif (ACCEL_ODR_HZ <= 50) // select 50Hz ODR
133  { FXOS8700_CTRL_REG1, 0x1D, 0x00 },
134 #elif (ACCEL_ODR_HZ <= 100) // select 100Hz ODR
135  { FXOS8700_CTRL_REG1, 0x15, 0x00 },
136 #elif (ACCEL_ODR_HZ <= 200) // select 200Hz ODR
137  { FXOS8700_CTRL_REG1, 0x0D, 0x00 },
138 #else // select 400Hz ODR
139  { FXOS8700_CTRL_REG1, 0x05, 0x00 },
140 #endif
141  __END_WRITE_DATA__
142 };
143 
144 #define FXOS8700_COUNTSPERG 8192.0
145 #define FXOS8700_COUNTSPERUT 10
146 
147 // All sensor drivers and initialization functions have the same prototype
148 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
149 
150 // sfg = pointer to top level (generally global) data structure for sensor fusion
152 {
153  int32_t status;
154  uint8_t reg;
155  status = Register_I2C_Read(sensor->bus_driver, sensor->addr, FXOS8700_WHO_AM_I, 1, &reg);
156 
157  if (status==SENSOR_ERROR_NONE) {
158 #if F_USING_ACCEL
159  sfg->Accel.iWhoAmI = reg;
160  sfg->Accel.iCountsPerg = FXOS8700_COUNTSPERG;
161  sfg->Accel.fgPerCount = 1.0F / FXOS8700_COUNTSPERG;
162 #endif
163 #if F_USING_MAG
164  sfg->Mag.iWhoAmI = reg;
166  sfg->Mag.fCountsPeruT = (float) FXOS8700_COUNTSPERUT;
167  sfg->Mag.fuTPerCount = 1.0F / FXOS8700_COUNTSPERUT;
168 #endif
169  if (reg != FXOS8700_WHO_AM_I_PROD_VALUE) {
170  return SENSOR_ERROR_INIT; // The whoAmI did not match
171  }
172  } else {
173  // whoAmI will rettain default value of zero
174  // return with error
175  return status;
176  }
177 
178  // Configure and start the fxos8700 sensor. This does multiple register writes
179  // (see FXOS8700_Initialization definition above)
180  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, FXOS8700_Initialization );
182 #if F_USING_ACCEL
183  sfg->Accel.isEnabled = true;
184 #endif
185 #if F_USING_MAG
186  sfg->Mag.isEnabled = true;
187 #endif
188 
189  return (status);
190 }
191 
192 #if F_USING_ACCEL
193 int8_t FXOS8700_ReadAccData(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
194 {
195  uint8_t I2C_Buffer[6 * FXOS8700_ACCEL_FIFO_SIZE]; // I2C read buffer
196  int32_t status; // I2C transaction status
197  int8_t j; // scratch
198  uint8_t sensor_fifo_count;
199  int16_t sample[3];
200 
201  if(!(sensor->isInitialized & F_USING_ACCEL))
202  {
203  return SENSOR_ERROR_INIT;
204  }
205 
206  // read the F_STATUS register (mapped to STATUS) and extract number of measurements available (lower 6 bits)
207  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, FXOS8700_F_STATUS_READ, I2C_Buffer );
208  if (status != SENSOR_ERROR_NONE) return(status);
209  else {
210  sensor_fifo_count = I2C_Buffer[0] & 0x3F;
211  // return if there are no measurements in the sensor FIFO.
212  // this will only occur when the FAST_LOOP_HZ equals or exceeds ACCEL_ODR_HZ
213  if (sensor_fifo_count == 0) return(SENSOR_ERROR_READ);
214  }
215 
216  FXOS8700_DATA_READ[0].readFrom = FXOS8700_OUT_X_MSB;
217  FXOS8700_DATA_READ[0].numBytes = 6 * sensor_fifo_count;
218  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, FXOS8700_DATA_READ, I2C_Buffer );
219 
220  if (status==SENSOR_ERROR_NONE) {
221  for (j = 0; j < sensor_fifo_count; j++) {
222  sample[CHX] = (I2C_Buffer[6 * j] << 8) | (I2C_Buffer[6 * j + 1]); // decode X
223  sample[CHY] = (I2C_Buffer[6 * j + 2] << 8) | (I2C_Buffer[6 * j + 3]); // decode Y
224  sample[CHZ] = (I2C_Buffer[6 * j + 4] << 8) | (I2C_Buffer[6 * j + 5]); // decode Z
225  conditionSample(sample); // truncate negative values to -32767
226  // place the 6 bytes read into the 16 bit accelerometer structure
227  addToFifo((FifoSensor*) &(sfg->Accel), ACCEL_FIFO_SIZE, sample);
228  }
229  }
230 
231  return (status);
232 }
233 #endif
234 #if F_USING_MAG
235 // read FXOS8700 magnetometer over I2C
237 {
238  uint8_t I2C_Buffer[6]; // I2C read buffer
239  int32_t status; // I2C transaction status
240  int16_t sample[3];
241 
242  if(!(sensor->isInitialized & F_USING_MAG))
243  {
244  return SENSOR_ERROR_INIT;
245  }
246 
247  // read the six sequential magnetometer output bytes
248  FXOS8700_DATA_READ[0].readFrom = FXOS8700_M_OUT_X_MSB;
249  FXOS8700_DATA_READ[0].numBytes = 6;
250  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, FXOS8700_DATA_READ, I2C_Buffer );
251  if (status==SENSOR_ERROR_NONE) {
252  // place the 6 bytes read into the magnetometer structure
253  sample[CHX] = (I2C_Buffer[0] << 8) | I2C_Buffer[1];
254  sample[CHY] = (I2C_Buffer[2] << 8) | I2C_Buffer[3];
255  sample[CHZ] = (I2C_Buffer[4] << 8) | I2C_Buffer[5];
256  conditionSample(sample); // truncate negative values to -32767
257  addToFifo((FifoSensor*) &(sfg->Mag), MAG_FIFO_SIZE, sample);
258  }
259  return status;
260 }
261 #endif
262 // This is the composite read function that handles both accel and mag portions of the FXOS8700
263 
264 // It returns the first failing status flag
266 {
267  int8_t sts1 = 0;
268  int8_t sts2 = 0;
269 #if F_USING_ACCEL
270  sts1 = FXOS8700_ReadAccData(sensor, sfg);
271 #endif
272 
273 #if F_USING_MAG
274  sts2 = FXOS8700_ReadMagData(sensor, sfg);
275 #endif
276 
277  if (sts1)
278  return (sts1);
279  else
280  return (sts2);
281 }
282 
283 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
284 const registerwritelist_t FXOS8700_FULL_IDLE[] =
285 {
286  // Set ACTIVE = other bits unchanged
287  { FXOS8700_CTRL_REG1, 0x00, 0x01 },
288  __END_WRITE_DATA__
289 };
290 
291 // FXOS8700_Idle places the entire sensor into STANDBY mode (wakeup time = 1/ODR+1ms)
292 // This driver is all-on or all-off. It does not support mag or accel only.
293 // If you want that functionality, you can write your own using the initialization
294 // function in this file as a starting template. We've chosen not to cover all
295 // permutations in the interest of simplicity.
297 {
298  int32_t status;
299  if(sensor->isInitialized == (F_USING_ACCEL|F_USING_MAG)) {
300  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, FXOS8700_FULL_IDLE );
301  sensor->isInitialized = 0;
302 #if F_USING_ACCEL
303  sfg->Accel.isEnabled = false;
304 #endif
305 #if F_USING_MAG
306  sfg->Mag.isEnabled = false;
307 #endif
308  } else {
309  return SENSOR_ERROR_INIT;
310  }
311  return status;
312 }
#define FXOS8700_COUNTSPERUT
const registerwritelist_t FXOS8700_FULL_IDLE[]
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:77
#define FXOS8700_ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
#define FXOS8700_COUNTSPERG
int8_t FXOS8700_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
void addToFifo(FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The top level fusion structure.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define MAG_FIFO_SIZE
FXOS8700 (mag), MAG3110 have no FIFO so equivalent to 1 element FIFO.
int16_t iCountsPeruT
counts per uT
#define ACCEL_FIFO_SIZE
FXOS8700 (accel), MMA8652, FXLS8952 all have 32 element FIFO.
MagSensor Mag
magnetometer storage
int8_t FXOS8700_ReadMagData(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
int8_t FXOS8700_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
float fuTPerCount
uT per count
bool isEnabled
true if the device is sampling
The sensor_fusion.h file implements the top level programming interface.
#define CHZ
const registerreadlist_t FXOS8700_WHO_AM_I_READ[]
const registerreadlist_t FXOS8700_F_STATUS_READ[]
Provides function prototypes for driver level interfaces.
const registerwritelist_t FXOS8700_Initialization[]
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:76
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
float fCountsPeruT
counts per uT
Application-specific status subsystem.
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
uint8_t iWhoAmI
sensor whoami
int8_t FXOS8700_Idle(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint16_t addr
I2C address if applicable.
#define F_USING_MAG
Definition: magnetic.h:38
registerreadlist_t FXOS8700_DATA_READ[]